package com.bluesimon.wbf.usercenter;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Created by Django on 2017/7/22.
 */
@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    public User getUser(String userId) {
        User u = userRepository.findOne(userId);
        return u;
    }

    public User getUserByName(String userName) {
        List<User> temp = userRepository.findByUserName(userName);
        if (null != temp && temp.size() > 0) {
            return temp.get(0);
        }
        return null;
    }


    public User login(String loginName, String password) {
        List<User> list = userRepository.findByUserNameAndPassword(loginName, password);
        if (null != list && list.size() > 0) {
            User u = list.get(0);
            return u;
        }
        return null;
    }

    @Transactional
    public User register(User user) {
        user.setId(UUID.randomUUID().toString());
        user = userRepository.saveAndFlush(user);
        return user;
    }

    @Transactional
    public User update(User user) {
        return userRepository.saveAndFlush(user);
    }
}
